<template>
 <div>
      <!-- 弹出层的头部区域 -->
  <van-nav-bar title="频道管理">
    <template #right>
      <van-icon name="cross" size="0.37333334rem" color="white" @click="crossCloseFn" />
    </template>
  </van-nav-bar>
  <!-- 我的频道 -->
  <div class="my-channel-box">
    <div class="channel-title">
      <span
        >我的频道
        <span class="small-title">
          点击{{ isShow? '删除':'进入' }}频道
        </span>
      </span>
      <span @click="editFn">{{ isShow? '完成':'编辑' }}</span>
    </div>
    <!-- 我的频道列表 -->
    <van-row type="flex">
        <!-- v-for循环生成频道列表徽章标-->
      <van-col span="6"
      v-for="obj in checkedChannelList"
      :key="obj.id"
      @click="enterChannelFn(obj.id)">
        <div class="channel-item van-hairline--surround">
          {{ obj.name }}
          <!-- 删除的徽标 -->
          <!-- van-badge组件本身没有事件，加.native修饰符，可以给标签绑定原生的事件 -->
          <van-badge
          color="transparent"
          class="cross-badge"
          v-show="isShow && obj.id !== 0"
          @click.native="deleteChannelFn(obj.id)">
            <template #content>
                <!-- 频道id为0时，即推荐频道不显示x号 -->
              <van-icon
                name="cross"
                class="badge-icon"
                color="#cfcfcf"
                size="0.32rem"
              />
            </template>
          </van-badge>
        </div>
      </van-col>
    </van-row>
  </div>
  <!-- 更多频道 -->
  <div class="more-channel-box">
    <div class="channel-title">
      <span>点击添加更多频道：</span>
    </div>
    <!-- 更多频道列表 -->
    <van-row type="flex">
      <van-col span="6" v-for="obj in addChannelList" :key="obj.id" @click="addFn(obj)">
        <div class="channel-item van-hairline--surround" >{{ obj.name }}</div>
      </van-col>
    </van-row>
  </div>
 </div>
</template>
<script>
import { getAllChannelListApi } from '@/api'
export default {
  props: {
    checkedChannelList: Array // 接收父组件传递的用户已选频道列表(props变量是只读的)
  },
  data () {
    return {
      allChannelList: [], // 所有频道列表
      isShow: false // 编辑x号的显示隐藏状态
    }
  },
  methods: {
    //   点击x关闭按钮，关闭弹层
    crossCloseFn () {
      // 触发父组件的自定义事件
      this.$emit('colseEvent', false)
      this.isShow = false
    },
    // 点击编辑按钮，x号的显示与隐藏进行切换
    editFn () {
      this.isShow = !this.isShow
    },
    // 添加频道，触发父组件的自定义事件
    addFn (obj) {
      // console.log(obj)
      if (this.isShow === true) {
        // this.checkedChannelList.push(obj)
        this.$emit('addEvent', obj)
      }
    },
    // 删除频道
    deleteChannelFn (channelId) {
      if (channelId !== 0) {
        this.$emit('deleteChannelEvent', channelId)
      }
    },
    // 进入频道,触发父组件的input事件，传递相应id
    enterChannelFn (channelId) {
      this.$emit('colseEvent', false) // 关闭整个弹出层
      this.$emit('input', channelId)
    }
  },
  //  初始化完成获取所有频道列表
  async created () {
    const res = await getAllChannelListApi()
    // console.log(res)
    this.allChannelList = res.data.data.channels
  },
  computed: {
    // 对所有频道列表进行筛选过滤，与用于已选频道比较，筛出不同的频道组成新的数组即为可以添加的更多频道
    // filter筛选数组中满足条件的元素,并组成一个新数组返回
    addChannelList () {
      return this.allChannelList.filter(allObj => {
        const index = this.checkedChannelList.findIndex(checkedObj => checkedObj.id === allObj.id)
        if (index === -1) {
          return true
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}

</style>
